# frozen_string_literal: true
class V1::ApplicationController < ActionController::Base
  layout 'v1/layouts/application'

  include AuthenticationPlugin
  include CommonErrorPlugin
  include ParamsPlugin

  rescue_from StandardError, with: :server_error if Rails.env.production?
  rescue_from CommonError::CustomMessageError, with: :error_422
  rescue_from CommonError::ModelValidationError, with: :error_422
  rescue_from ActionController::ParameterMissing, with: :error_422
  rescue_from ActiveRecord::RecordNotFound, with: :error_404

  protected

    def server_error(e)
      render json: { error: 'HTTP Version Not Supported' }.to_json, status: 505
    end

    def error_422(e)
      render json: { error: e.message }.to_json, status: 422
    end

    def error_433
       render json: { error: '设备响应超时' }.to_json, status: 433
    end

    def error_401(e)
      render json: { error: e.message }.to_json, status: 401
    end

    def error_404
      render json: { error: '不存在记录' }.to_json, status: 404
    end
end
